package edu.nf.ch06.web;

import edu.nf.ch06.entity.User;
import edu.nf.ch06.exception.GlobalException;
import edu.nf.ch06.exception.LoginException;
import edu.nf.ch06.service.AuthService;
import edu.nf.ch06.web.vo.ResultVO;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangl
 * @date 2025/5/13
 */
@RestController
public class AuthController extends BaseController {

    private AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    /**
     * 使用@ExceptionHandler注解的方法是异常处理方法，
     * 如果在controller中定义异常处理方法，那么这些方法
     * 处理当前的controller方法的异常（局部异常处理）
     * @param e
     * @return
     * @param <T>
     */
    /*@ExceptionHandler(LoginException.class)
    public <T> ResultVO<T> handleGlobalException(LoginException e) {
        e.printStackTrace();
        ResultVO<T> vo = new ResultVO<>();
        vo.setCode(e.getErrorCode());
        vo.setMessage(e.getMessage());
        return vo;
    }*/

    @GetMapping("/auth")
    public ResultVO<User> auth(@RequestParam("account") String account,
                               @RequestParam("password") String password) {
        User user = authService.auth(account, password);
        return success(user);
    }
}